package com.ghck.shop.dao;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.ghck.shop.constant.GoodStatus;
import com.ghck.shop.dao.provider.GoodsMapperProvider;
import com.ghck.shop.model.Category;
import com.ghck.shop.model.CommentLabel;
import com.ghck.shop.model.Goods;
import com.ghck.shop.model.GoodsImage;
import com.ghck.shop.model.GoodsSortConfig;
import com.ghck.shop.model.MarketingActivityGoods;
import com.ghck.shop.model.PostageConfig;
import com.ghck.shop.model.SystemCity;
import com.ghck.shop.model.request.GoodsCommentReq;
import com.ghck.shop.model.request.GoodsReq;
import com.ghck.shop.model.response.CategoryRes;
import com.ghck.shop.model.response.GoodCollectRes;
import com.ghck.shop.model.response.GoodsRes;
import com.ghck.shop.model.response.MarketingActivityRes;


/** 
 * @author 作者 : chenxuan
 * @version 创建时间：2015年12月30日 下午1:27:05 
 * 类说明 
 */
public interface GoodsMapper {
	
	@Update("update goods set amount = amount - #{num} where id = #{goodsId}")
	public void subtractStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	
	@Update("update goods set amount = amount + #{num} where id = #{goodsId}")
	public void increaseStock(@Param("goodsId") Integer goodsId,@Param("num") Integer num);
	
	
	@Update("update goods set buy_count = (select buy_count + b.goods_number from order_info a,order_detail b where a.id = b.order_id and a.order_no "
			+ " = #{orderNo} and b.is_service != 1 and goods.id = b.goods_id ) where "
			+ " (select 1 from order_info a,order_detail b where a.id = b.order_id and a.order_no = #{orderNo} "
			+ " and b.is_service != 1 and goods.id = b.goods_id )")
	public Integer updateGoodsBuyCountByOrderNo(String orderNo);
	
	/**
	 * 评价商品
	 * @param orderCommentReq
	 * @return
	 */
	@Insert("insert into shop_comment(order_id,goods_id,user_id,is_manager,comment,goods_source,"
			+ " shipping_source,help_source,avg_source,is_open) values "
			+ " (#{orderId},#{goodsId},#{userId},#{isManager},#{comment},#{goodsSource},"
			+ " #{shippingSource},#{helpSource},#{avgSource},#{isOpen})")
	@Options(useGeneratedKeys=true)
	public Integer addShopComment(GoodsCommentReq goodsCommentReq);
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsHomeList")
	public List<GoodsRes> getHomeList(GoodsReq goodsReq);
	
	@Select("select * from goods where id = #{id}")
	public GoodsRes getGoodsDetailById(Integer id);
	
	@Select("select * from goods where id = #{id} and supplier_id=#{supplierId}")
	public GoodsRes getGoodsByIdAndSupplierId(@Param("id")Integer id,@Param("supplierId")Integer supplierId);
	
	@Update("update goods set is_onsale="+GoodStatus.is_delete+" where id=#{id}")
	public Integer deleteGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.in_review + " where id=#{id}")
	public Integer revieweGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.init + " where id=#{id}")
	public Integer cancelRevieweGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.review_success + " where id=#{id}")
	public Integer reviewSuccessGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.review_fail + " where id=#{id}")
	public Integer reviewFailGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.on_sale + " where id=#{id}")
	public Integer shelvesGoodById(@Param("id")Integer id);
	
	@Update("update goods set is_onsale="+GoodStatus.off_sale + " where id=#{id}")
	public Integer xiajiaGoodById(@Param("id")Integer id);
	
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="findGoodsBySupplierId")
	public List<GoodsRes> findGoodsBySupplierId(@Param("supplierId")Integer supplierId,@Param("isOnsale")Integer isOnsale,@Param("start")Integer start,@Param("limit")Integer limit);
	
	@Select("select * from goods_image where goods_id = #{goodsId} and status = 1")
	public List<GoodsImage> getGoodsImageList(Integer goodsId);
	@Select("select * from goods_image where id=#{imageId}")
	public GoodsImage findGoodsImageById(@Param("imageId")Integer imageId);
	
	@Delete("delete from goods_image where id=#{imageId}")
	public Integer deleteGoodsImageById(@Param("imageId")Integer imageId);
	
	@Delete("delete from goods_image where goods_id=#{goodId}")
	public Integer deleteGoodsImageByGoodId(@Param("goodId")Integer goodId);
	
	@DeleteProvider(type=GoodsMapperProvider.class,method="deleteGoodsImageByGoodIdAndNotIds")
	public Integer deleteGoodsImageByGoodIdAndNotIds(@Param("goodId")Integer goodId,@Param("ids")List<Integer> ids);
	
	
	@InsertProvider(type=GoodsMapperProvider.class,method="batchSaveGoodsImages")
	public void batchSaveGoodsImages(List<GoodsImage> list);
	
	@InsertProvider(type=GoodsMapperProvider.class,method="batchSaveGoodsCategory")
	public void batchSaveGoodsCategory(@Param("goodId")Integer goodId,@Param("categoryIds")List<Integer> categoryIds);
	
	@Select("select * from t_system_city")
	public List<SystemCity> getAllRegion();
	
	/**
	 * 根据省ID获取邮费配置信息
	 * @param provinceId
	 * @return
	 */
	@Select("select * from postage_config where province_id = #{provinceId}")
	public PostageConfig getPostageConfigByProvinceId(Integer provinceId);
	
	/**
	 * 只用大于100的ID，小于100的用于自定义
	 * @return
	 */
	@Select("select * from category where status = 1 order by sort_order desc ")
	public List<Category> categoryList();
	
	@Select("select * from category order by sort_order desc ")
	public List<Category> allCategoryList();
	
	@Insert("insert into category(title,image,level,parent_id,shop_use,create_time,last_modify_time)values(#{title},#{image},#{level},#{parentId},#{shopUse},now(),now())")
	public Integer saveCategory(Category category);
	
	@Select("select * from category where status = 1 and parent_id > 0 order by sort_order desc ")
	public List<Category> categoryList4Good();
	
	@Select("select * from category where status = 1 and parent_id > 0 and shop_use=1 order by sort_order desc ")
	public List<Category> categoryList4ShopGood();
	
	@Update("update category set shop_use=1 where id=#{id}")
	public Integer updateCategoryShopUse(Integer id);
	@Update("update category set shop_use=0 where id=#{id}")
	public Integer cancelCategoryShopUse(Integer id);
	
	@Select("select id,name,seq from goods_sort_config order by seq desc")
	public List<GoodsSortConfig> sortList();
	
	@Select("select id,name,seq,config from goods_sort_config where id = #{id}")
	public GoodsSortConfig getGoodsSortConfig(Integer id);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsList")
	public List<GoodsRes> goodsList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="goodsCollectList")
	public List<GoodCollectRes> goodsCollectList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="medtronicGoodsList")
	public List<GoodsRes> medtronicGoodsList(GoodsReq goodsReq);
	
	@Select("select * from marketing_activity where id = #{id}")
	public MarketingActivityRes queryMarketingActivityById(Integer id);
	
	
	@Select("select * from marketing_activity where type = #{type} order by last_modify_time desc limit 1")
	public MarketingActivityRes queryMarketingActivityByType(Integer type);
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="marketingActivityGoodsList")
	public List<GoodsRes> marketingActivityGoodsList(GoodsReq goodsReq);
	/**
	 * 评价标签列表
	 * @return
	 */
	@Select("select id,name from comment_label where in_used = 1")
	public List<CommentLabel> commentLabelList();
	
	@Select("select id,title,image from category where recommend = 1 order by sort_order desc")
	public List<CategoryRes> homeCategoryList();
	
	
	
	@SelectProvider(type=GoodsMapperProvider.class,method="getCategoryByIds")
	public List<CategoryRes> getCategoryByIds(List<Integer> list);
	
	@Select("select a.* from goods a left join goods_category b on a.id = b.goods_id where b.category_id  = #{categoryId}")
	public List<GoodsRes> findGoodsByCategoryId(GoodsReq goodsReq);
	
	@Select("select g.* from goods g left join t_seller ts on g.supplier_id=ts.id where is_onsale=3 and ( g.title like '%#{keyword}%' or ts.shop_name like '%#{keyword}%')")
	public List<GoodsRes> findGoods(GoodsReq goodsReq);
	
	@Select("select t2.* from goods_category t1,category t2 where t2.id=t1.category_id and t1.goods_id=#{goodId}")
	public List<Category> findGoodCategoryByGoodId(Integer goodId);
	/**
	 * 管理平台商品列表
	 * @param goodsReq
	 * @return
	 */
	@SelectProvider(type=GoodsMapperProvider.class,method="managerList")
	public List<GoodsRes> managerList(GoodsReq goodsReq);
	
	@SelectProvider(type=GoodsMapperProvider.class,method="countManagerList")
	public Integer countManagerList(GoodsReq goodsReq);
	
	@Options(useGeneratedKeys = true)
	@InsertProvider(type=GoodsMapperProvider.class,method="saveGoods")
	public Integer saveGoods(Goods goods);
	
	@Update("update category set recommend=1 where id=#{categoryId}")
	public Integer addFirstHomeComment( Integer categoryId);
	
	@Update("update category set recommend=0 where id=#{categoryId}")
	public Integer cancelFirstHomeComment(Integer categoryId);
	
	@Insert("insert into t_hot_category(category_id,status,create_time,update_time)values(#{categoryId},1,now(),now())")
	public Integer addHotSell(Integer categoryId);
	
	@Update("delete from  t_hot_category where category_id=#{categoryId}")
	public Integer cancelHotSell(Integer categoryId);
	/**
	 * 根据商品id删除商品所属分类
	 * @param goodId
	 * @return
	 */
	@Delete("delete from goods_category where goods_id=#{goodId}")
	public Integer deleteGoodCategorysByGoodId(Integer goodId);
	
	/**
 	*更新商品 
 	*/
 	@UpdateProvider(method="update",type=GoodsMapperProvider.class)
 	public Integer updateGoods(Goods domain);
 	
 	@Update("update category set image = #{path} where id = #{categoryId}")
 	public Integer updateCategoryImage(@Param("categoryId") Integer categoryId,@Param("path") String path);
 	
 	@Insert("insert into marketing_activity_goods(goods_id,marketing_activity_id) values(#{goodsId},#{marketingActivityId})")
	public void addMarketingActivity(@Param("goodsId")Integer goodsId,@Param("marketingActivityId") Integer marketingActivityId) ;

 	@Delete("delete from marketing_activity_goods where goods_id = #{goodsId} and marketing_activity_id = #{marketingActivityId}")
	public void deleteMarketingActivity(@Param("goodsId")Integer goodsId,@Param("marketingActivityId") Integer marketingActivityId);
 	
 	@Select("select * from marketing_activity_goods where goods_id = #{goodId} ")
	public List<MarketingActivityGoods> findGoodMarketingActivityByGoodId(Integer goodId);
 	
	
}